package com.example.teacher.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.teacher.R
import com.example.teacher.data.Course

/**
 *@Created by 覃德玮 on 2021/9/18 17:23
 *@Email: chadewik@foxmail.com
 *@Description:
 */
class RecyclerCourseAdapter(mContext: Context, listData: List<Course>) :
    RecyclerView.Adapter<RecyclerCourseAdapter.RecyclerHolder>() {

    private var list: List<Course>
    private var context: Context
    private var itemClickListener: RecyclerItemClickListener? = null


    init {
        this.context = mContext
        this.list = listData
    }


    class RecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView = itemView.findViewById(R.id.coursename)
        val id: TextView =itemView.findViewById(R.id.courseid)
        val teacher: TextView = itemView.findViewById(R.id.teacher)
        val hours: TextView = itemView.findViewById(R.id.hours)
        val stunum: TextView = itemView.findViewById(R.id.stunum)
    }

    //自定义接口
    interface RecyclerItemClickListener {
        fun onItemClickListener(position: Int)
    }

    fun setOnRecyclerItemClickListener(itemClickListener: RecyclerItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerHolder {
        var view =
            LayoutInflater.from(context).inflate(R.layout.course_list_item, parent, false)
        return RecyclerHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerHolder, position: Int) {
        holder.name.text=list[position].courseName
        holder.id.text=list[position].courseId
        holder.teacher.text=list[position].teacher
        holder.hours.text=list[position].classHours.toString()
        holder.stunum.text=list[position].courseNum.toString()
        holder.itemView.setOnClickListener {
            itemClickListener!!.onItemClickListener(position)

//            Toast.makeText(this.context,position,Toast.LENGTH_SHORT).show()

        }
    }

    override fun getItemCount(): Int {
        return list.size
    }
}